/* eslint-disable @typescript-eslint/no-unused-vars */
import axios from 'axios';
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
import { showMsg } from '@/uitl/toast';

const $api = axios.create({
  baseURL: 'http://127.0.0.1:3030',
});

// 请求拦截
$api.interceptors.request.use((config: AxiosRequestConfig) => {
  const actk = window.localStorage.getItem('accessToken');
  // eslint-disable-next-line no-param-reassign
  if (actk) config.headers.Authorization = `bearer ${actk}`;
  return config;
});

// 响应拦截
$api.interceptors.response.use(
  (response: AxiosResponse) => response,
  (err) => {
    let message = '';
    switch (err.response.status) {
      case 401:
        message = '未授权请重新登录';
        break;
      case 403:
        message = '服务器拒绝访问';
        break;
      default:
        message = `请求错误${err.request.status}`;
    }

    showMsg(message);
  },
);
export default $api;
